﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using binary.Classes;

namespace binary.Helpers
{
    public static class ImportHelper
    {
        public static void DoMultiImport(string xmlfile, bool autodate)
        {
            DateTime start;
            DialogResult importResult = DialogResult.Abort;
            ConfigList list = XMLReader.getImportList(xmlfile);
            string adDestination = "";
            if (autodate)
            {
                Autodate ad = AutodateHelper.GetDate();
                adDestination = string.Format("{0}{1}", ad.Location, ad.Directory);
                AutodateHelper.IncrementDate();
            }
            string fileLocation = "";

            if (list.IsValid())
            {
                int i = 1;
                mySqlConnection connection = new mySqlConnection(list.getConfig());

                foreach (SingleTable table in list.tableList)
                {
                    start = DateTime.Now;

                    fileLocation = Path.Combine((autodate == true ? adDestination : list.dbfDestination), String.Format("{0}{1}",table.name, ".dbf"));
                    if (File.Exists(fileLocation))
                    {
                        using (ImportTable form = new ImportTable(list.getConfig(), fileLocation, table, i, list.tableList.Count))
                        {
                            importResult = form.ShowDialog();
                            if (importResult != DialogResult.Abort)
                            {
                                //if (form.dbf.ListaBledow != null)
                                    //if (form.dbf.ListaBledow.Count > 0)
                                        //connection.insertErrors((list.getConfig()).errorsTable, form.dbf.ListaBledow);
                                


                                ImportLog log = new ImportLog // tworzy nowy obiekt wpisu do loga
                                {
                                    DbfRecords = form.dbf.LiczRekNieusunietych,
                                    SqlRecords = connection.getRowsNumber(table.destinationName),
                                    LoadingStart = start,
                                    LoadingFinish = DateTime.Now,
                                    LoadingTime = DateTime.Now.Subtract(start).TotalSeconds,
                                    Table = table.destinationName
                                };
                                //connection.insertImportLog(list.getConfig().logTable, log); // dodaj wpis do loga o załadowanej tabeli
                            }
                        }
                    }
                    i++;
                }
                if (!String.IsNullOrEmpty(list.finish) && importResult != DialogResult.Abort)
                {
                    ExecProcedure form = new ExecProcedure(list.getConfig(), list.finish);
                    form.ShowDialog();
                }
            }
            else
                MessageBox.Show(String.Format("Brak pola: {0}", list.getNonValidField()), "Nieprawidłowy plik konfiguracyjny"); 
        }
    }
}
